package lumis.portal.serviceinterface;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Collection;

import lumis.portal.*;
import lumis.portal.dao.*;
import lumis.portal.dao.jdbc.ITransactionJdbc;
import lumis.util.ITransaction;
import lumis.util.JdbcUtil;
/**
 * 
 * @since 4.0.0
 * @version $Revision: 12877 $ $Date: 2011-05-02 16:20:30 -0300 (Mon, 02 May 2011) $
 */
public class ServiceInterfaceDaoJdbc implements IServiceInterfaceDao
{
	public ServiceInterfaceConfig get(String serviceInterfaceId, ITransaction daoTransaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) daoTransaction;
			Connection connection = daoTransactionJdbc.getConnection();
			
			ServiceInterfaceConfig loadedInterface = null;
			String strQuery = "SELECT definition FROM lum_Interface WHERE interfaceId=?";

			PreparedStatement statement = connection.prepareStatement(strQuery);

			statement.setString(1, serviceInterfaceId);
			ResultSet resultSet = statement.executeQuery();

			if (resultSet.next())
			{
				loadedInterface = new ServiceInterfaceConfig();
				loadedInterface.setDefinition(resultSet.getString("definition"));
			}

			resultSet.close();
			statement.close();

			if (loadedInterface == null)
				throw new DaoException("STR_INTERFACE_DOES_NOT_EXIST;" + serviceInterfaceId);

			return loadedInterface;
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}

	public void addOrUpdate(ServiceInterfaceConfig serviceInterfaceConfig, ITransaction daoTransaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) daoTransaction;
			Connection connection = daoTransactionJdbc.getConnection();

			// first try to update
			PreparedStatement statement = connection.prepareStatement("update lum_Interface set name=?, serviceId=?, description=?, baseUrl=?, stringsUrlPrefix=?, definition=?, isRuntimeOnly=?, isAdministration=?, isPrintable=?, renderAs=?, enableCache=?, cacheLevel=?, cachePersonalization=?, cacheServerSideInclude=?, title=?, isDetails=?, previewSupported=?, isWidget=? where interfaceId=?");

			statement.setString(1, serviceInterfaceConfig.getName());
			statement.setString(2, serviceInterfaceConfig.getServiceId());
			statement.setString(3, serviceInterfaceConfig.getDescription());
			statement.setString(4, serviceInterfaceConfig.getBaseUrl());
			statement.setString(5, serviceInterfaceConfig.getStringsUrlPrefix());
			statement.setString(6, serviceInterfaceConfig.getDefinition());
			statement.setInt(7, serviceInterfaceConfig.isRuntimeOnly() ? 1 : 0);
			statement.setInt(8, serviceInterfaceConfig.isAdministration() ? 1 : 0);
			statement.setInt(9, serviceInterfaceConfig.isPrintable() ? 1 : 0);			
			statement.setInt(10, serviceInterfaceConfig.getRenderAs());
			statement.setInt(11, serviceInterfaceConfig.isEnableCache() ? 1 : 0);
			statement.setInt(12, serviceInterfaceConfig.getCacheLevel());
			statement.setInt(13, serviceInterfaceConfig.getCachePersonalization());
			statement.setInt(14, serviceInterfaceConfig.isCacheServerSideInclude() ? 1 : 0);
			statement.setString(15, serviceInterfaceConfig.getTitle());
			statement.setInt(16, serviceInterfaceConfig.isDetails() ? 1 : 0);
			statement.setInt(17, serviceInterfaceConfig.isPreviewSupported() ? 1 : 0);
			statement.setInt(18, serviceInterfaceConfig.getIsWidget() ? 1 : 0);
			
			statement.setString(19, serviceInterfaceConfig.getId());

			int rowsAffected = statement.executeUpdate();
			statement.close();

			if (rowsAffected <= 0)
			{
				// row does not exist, so do insert
				statement = connection.prepareStatement("insert into lum_Interface (interfaceId, name, serviceId, description, baseUrl, stringsUrlPrefix, isRuntimeOnly, isAdministration, isPrintable, definition, renderAs, enableCache, cacheLevel, cachePersonalization, cacheServerSideInclude, title, isDetails, previewSupported, isWidget) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

				statement.setString(1, serviceInterfaceConfig.getId());
				statement.setString(2, serviceInterfaceConfig.getName());
				statement.setString(3, serviceInterfaceConfig.getServiceId());
				statement.setString(4, serviceInterfaceConfig.getDescription());
				statement.setString(5, serviceInterfaceConfig.getBaseUrl());
				statement.setString(6, serviceInterfaceConfig.getStringsUrlPrefix());
				statement.setInt(7, serviceInterfaceConfig.isRuntimeOnly() ? 1 : 0);
				statement.setInt(8, serviceInterfaceConfig.isAdministration() ? 1 : 0);
				statement.setInt(9, serviceInterfaceConfig.isPrintable() ? 1 : 0);
				statement.setString(10, serviceInterfaceConfig.getDefinition());
				statement.setInt(11, serviceInterfaceConfig.getRenderAs());
				statement.setInt(12, serviceInterfaceConfig.isEnableCache() ? 1 : 0);
				statement.setInt(13, serviceInterfaceConfig.getCacheLevel());
				statement.setInt(14, serviceInterfaceConfig.getCachePersonalization());
				statement.setInt(15, serviceInterfaceConfig.isCacheServerSideInclude() ? 1 : 0);
				statement.setString(16, serviceInterfaceConfig.getTitle());
				statement.setInt(17, serviceInterfaceConfig.isDetails() ? 1 : 0);
				statement.setInt(18, serviceInterfaceConfig.isPreviewSupported() ? 1 : 0);
				statement.setInt(19, serviceInterfaceConfig.getIsWidget() ? 1 : 0);

				statement.executeUpdate();
				statement.close();
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}

	public boolean delete(ServiceInterfaceConfig serviceInterfaceConfig, ITransaction daoTransaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) daoTransaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("delete from lum_Interface where interfaceId=?");
			try
			{
				statement.setString(1, serviceInterfaceConfig.getId());
				return statement.executeUpdate() > 0;
			}
			finally
			{
				statement.close();
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}

	public void setDefaultStyleId(String serviceInterfaceId, String serviceInterfaceStyleId, ITransaction daoTransaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) daoTransaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("update lum_Interface set defaultInterfaceStyleId=? where interfaceId=?");

			statement.setString(1, serviceInterfaceStyleId);
			statement.setString(2, serviceInterfaceId);

			int rowsAffected = statement.executeUpdate();
			statement.close();

			if (rowsAffected <= 0)
			{
				throw new DaoException("STR_INTERFACE_DOES_NOT_EXIST;" + serviceInterfaceId);
}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}
	public String getDefaultStyleId(String serviceInterfaceId, ITransaction daoTransaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) daoTransaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("select defaultInterfaceStyleId from lum_Interface where interfaceId=?");

			statement.setString(1, serviceInterfaceId);
			ResultSet resultSet = statement.executeQuery();

			String defaultStyleId = null;
			if (resultSet.next())
				defaultStyleId = resultSet.getString("defaultInterfaceStyleId");
			else
				throw new DaoException("STR_INTERFACE_DOES_NOT_EXIST;" + serviceInterfaceId);
				
			resultSet.close();
			statement.close();
			
			return defaultStyleId;
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}

	public void setCustomDefaultStyleId(String serviceInterfaceId, String serviceInterfaceStyleId, ITransaction daoTransaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) daoTransaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("update lum_Interface set customDefaultInterfaceStyleId=? where interfaceId=?");

			statement.setString(1, serviceInterfaceStyleId);
			statement.setString(2, serviceInterfaceId);

			int rowsAffected = statement.executeUpdate();
			statement.close();

			if (rowsAffected <= 0)
			{
				throw new DaoException("STR_INTERFACE_DOES_NOT_EXIST;" + serviceInterfaceId);
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}

	public String getCustomDefaultStyleId(String serviceInterfaceId, ITransaction daoTransaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) daoTransaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("select customDefaultInterfaceStyleId from lum_Interface where interfaceId=?");

			statement.setString(1, serviceInterfaceId);
			ResultSet resultSet = statement.executeQuery();

			String defaultStyleId = null;
			if (resultSet.next())
				defaultStyleId = resultSet.getString("customDefaultInterfaceStyleId");
			else
				throw new DaoException("STR_INTERFACE_DOES_NOT_EXIST;" + serviceInterfaceId);
				
			resultSet.close();
			statement.close();
			
			return defaultStyleId;
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}
	
	public Collection<String> getIdsByServiceId(String serviceId, ITransaction daoTransaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) daoTransaction;
			Connection connection = daoTransactionJdbc.getConnection();
			
			String strQuery = "select interfaceId from lum_Interface where serviceid = ?";

			PreparedStatement statement = connection.prepareStatement(strQuery);
			try
			{
				statement.setString(1, serviceId);
				return JdbcUtil.fillListString(statement, "interfaceId"); 				
			}
			finally
			{
				statement.close();				
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}
}
